﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using FingerSC;

namespace NewFinger
{
    public partial class Form1 : Form
    {
        //private test t1 = new test();
        private FingerSC.FingerSC FSC = new FingerSC.FingerSC();

        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            string[] str = new string[10];
            FSC.GetPort(ref str);
            foreach (string s in str)
                cbSerial.Items.Add(s);
        }

        private void FSC_Alarm(object sender, EventArgs e)
        {
            FingerSC.FingerSC.FingerEventArgs re = (FingerSC.FingerSC.FingerEventArgs)e;
            StringBuilder sbLog = new StringBuilder();
            sbLog.Append("WorkingModel:" + re.WorkingModel);
            sbLog.Append("State:" + re.State);
            sbLog.Append("ResID:" + re.ResID);
            Console.WriteLine(sbLog.ToString());
            textBox4.Text = re.strReturn;

            //textBox4.Text = sender.ToString();
            return;
        }

        private void FSC_FingerPrtTmp(object sneder,object sender1, object sender2, EventArgs e)
        {
            textBox5.Text = sender1.ToString();
            textBox6.Text = sender2.ToString();
            return;
        }
        private Bitmap RawToBitmap(byte[] RawSource, int width, int height)
        {
            Bitmap bm = new Bitmap(width,height);
            byte input;
            //注意：指纹仪上传的图像数据是Raw格式的，需要转换为BMP格式的图像才能在pictureBox中显示。
            //Raw数据是按行存储的。
            //本函数可以写入到FingerSC中。
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    input = RawSource[j*width + i];
                    bm.SetPixel(i, j, Color.FromArgb(input, input, input));
                }
            }
            return bm;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            
            int startID, endID, modelID;
            string FingerPrtTmp1, FingerPrtTmp2;
            startID = System.Convert.ToInt16( textBox1.Text);
            endID = System.Convert.ToInt16(textBox2.Text);
            modelID = System.Convert.ToInt16(textBox3.Text);
            FingerPrtTmp1 = textBox5.Text;
            FingerPrtTmp2 = textBox6.Text;
            if (radioButton1.Checked)//中断指令
                FSC.StopLastCmd();
            if (radioButton2.Checked)//匹配指纹
                FSC.MatchFinger(startID, endID);
            if (radioButton3.Checked)//注册指纹
                FSC.RegNewFinger(startID);
            if (radioButton4.Checked)//删除指纹
                FSC.DeleterFinger(startID, endID);
            if (radioButton5.Checked)//采集图像
                FSC.CollectFinger();
            if (radioButton6.Checked)//模板传输
                FSC.GetFingerModel(startID, modelID);
            if (radioButton7.Checked)//指纹数目
                FSC.GetFingerNumber(startID,endID );
            if (radioButton8.Checked)//下发指纹
                FSC.DownLoadFinger(startID, modelID, FingerPrtTmp1, FingerPrtTmp2);
        }

        private void btnSwitch_Click(object sender, EventArgs e)
        {
            if(!FSC.IsOpen())
            {
                if(cbSerial.Text=="")
                {
                    MessageBox.Show("请选择串口！", "Error");
                    return;
                }
                FSC.SetConf("Port", cbSerial.Text);
                FSC.initial();
                FSC.Alarm += new FingerSC.FingerSC.AlarmEventHandler(FSC_Alarm);
                FSC.FingerPrtTmp += new FingerSC.FingerSC.FingerPrtTmpEventHandler(FSC_FingerPrtTmp);
                FSC.FingerDeviceCallbackEvent += FSC_FingerDeviceCallbackEvent;
                btnSwitch.Text = "关闭串口";
            }
            else
            {
                FSC.ClosePort();
                btnSwitch.Text = "打开串口";
            }
        }

        private void FSC_FingerDeviceCallbackEvent(object sender, FingerSC.FingerDeviceCallbackValue FingerDeviceCallbackValue)
        {
            //switch (FingerDeviceCallbackValue.CallbackType)
            //{ } 
            switch (FingerDeviceCallbackValue.CallbackType)
            {
                case Enum_FingerDeviceCallbackType.Undefined:
                    break;
                case Enum_FingerDeviceCallbackType.Register_FirstEntrying:
                    break;
                case Enum_FingerDeviceCallbackType.Register_FirstEntryend:
                    break;
                case Enum_FingerDeviceCallbackType.Register_SencondEntrying:
                    break;
                case Enum_FingerDeviceCallbackType.Register_SencondEntryend:
                    break;
                case Enum_FingerDeviceCallbackType.Register_ThirdEntrying:
                    break;
                case Enum_FingerDeviceCallbackType.Register_ThirdEntryend:
                    break;
                case Enum_FingerDeviceCallbackType.Register_EntrySuccess:
                    break;
                case Enum_FingerDeviceCallbackType.Register_EntryFail:
                    break;
                case Enum_FingerDeviceCallbackType.DownloadModel_RequestModelData1:
                    break;
                case Enum_FingerDeviceCallbackType.DownloadModel_DownModelData1:
                    break;
                case Enum_FingerDeviceCallbackType.DownloadModel_RequestModelData2:
                    break;
                case Enum_FingerDeviceCallbackType.DownloadModel_DownModelData2:
                    break;
                case Enum_FingerDeviceCallbackType.DonwloadModel_DownModelDataSuccess:
                    break;
                case Enum_FingerDeviceCallbackType.DonwloadModel_DownModelDataFail:
                    break;
                case Enum_FingerDeviceCallbackType.RequestModel_Success:
                    break;
                default:
                    break;
            }
            Console.WriteLine(FingerDeviceCallbackValue.MessgeaInfo);
        }

        private void btnRefurbish_Click(object sender, EventArgs e)
        {
            cbSerial.Items.Clear();
            string[] str = new string[10];
            FSC.GetPort(ref str);
            if (str.Length == 0)
                MessageBox.Show("本机没有串口！", "Error");
            foreach (string s in str)
                cbSerial.Items.Add(s);
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (!FSC.IsOpen()&& radioButton1.Checked)
            {
                MessageBox.Show("未打开串口！", "Error");
                radioButton1.Checked = false;
            }
            if (radioButton1.Checked)
            {
                textBox1.Enabled = false;
                textBox2.Enabled = false;
                textBox3.Enabled = false;
            }
        }

        private void radioButton7_CheckedChanged(object sender, EventArgs e)
        {
            if (!FSC.IsOpen() && radioButton7.Checked)
            {
                MessageBox.Show("未打开串口！", "Error");
                radioButton7.Checked = false;
            }
            if(radioButton7.Checked)
            {
                textBox1.Enabled = true;
                textBox2.Enabled = true;
                textBox3.Enabled = false;
            }
        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            if (!FSC.IsOpen() && radioButton5.Checked)
            {
                MessageBox.Show("未打开串口！", "Error");
                radioButton5.Checked = false;
            }
            if (radioButton5.Checked)
            {
                textBox1.Enabled = false;
                textBox2.Enabled = false;
                textBox3.Enabled = false;
            }
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (!FSC.IsOpen() && radioButton3.Checked)
            {
                MessageBox.Show("未打开串口！", "Error");
                radioButton3.Checked = false;
            }
            if (radioButton3.Checked)
            {
                textBox1.Enabled = true;
                textBox2.Enabled = false;
                textBox3.Enabled = false;
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (!FSC.IsOpen() && radioButton2.Checked)
            {
                MessageBox.Show("未打开串口！", "Error");
                radioButton2.Checked = false;
            }
            if (radioButton2.Checked)
            {
                textBox1.Enabled = true;
                textBox2.Enabled = true;
                textBox3.Enabled = false;
            }
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (!FSC.IsOpen() && radioButton4.Checked)
            {
                MessageBox.Show("未打开串口！", "Error");
                radioButton4.Checked = false;
            }
            if (radioButton4.Checked)
            {
                textBox1.Enabled = true;
                textBox2.Enabled = true;
                textBox3.Enabled = false;
            }
        }

        private void radioButton6_CheckedChanged(object sender, EventArgs e)
        {
            if (!FSC.IsOpen() && radioButton6.Checked)
            {
                MessageBox.Show("未打开串口！", "Error");
                radioButton6.Checked = false;
            }
            if (radioButton6.Checked)
            {
                textBox1.Enabled = true;
                textBox2.Enabled = false;
                textBox3.Enabled = true;
            }
        }

        private void radioButton8_CheckedChanged(object sender, EventArgs e)
        {
            if (!FSC.IsOpen() && radioButton8.Checked)
            {
                MessageBox.Show("未打开串口！", "Error");
                radioButton8.Checked = false;
            }
            if (radioButton8.Checked)
            {
                textBox1.Enabled = true;
                textBox2.Enabled = false;
                textBox3.Enabled = true;
                textBox5.ReadOnly = false;
                textBox6.ReadOnly = false;
            }
            else
            {
                textBox5.ReadOnly = true;
                textBox6.ReadOnly = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Bitmap bm = RawToBitmap(FSC.GetFingerPic(), FSC.GetPicWidth(), FSC.GetPicHeight());
            pictureBox1.Image = bm;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            FSC.GetDevID();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            FSC.GetSecuClass();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            FSC.GetDevcNum();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {
            //1.

        }
    }
}
